extends "res://BaseEnemy.gd"

# 森林哥布林特有属性
@export var throw_cooldown = 3.0  # 投掷冷却时间
@export var throw_damage = 8  # 投掷伤害
@export var throw_range = 150.0  # 投掷范围

var throw_timer = 0.0
var can_throw = true

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置森林哥布林特有属性
	max_health = 25
	current_health = max_health
	movement_speed = 110.0
	damage = 7
	experience_value = 5
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.7, 0.9, 0.7)  # 森林色调

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理投掷冷却
	if not can_throw:
		throw_timer += delta
		if throw_timer >= throw_cooldown:
			can_throw = true
			throw_timer = 0.0
	
	# 如果可以投掷且玩家存在，尝试投掷
	if can_throw and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= throw_range and distance > 50:  # 在适当距离时投掷
			throw_rock()

func throw_rock():
	if animation_player and animation_player.has_animation("throw"):
		animation_player.play("throw")
		
		# 创建石头投射物
		var rock = preload("res://RockProjectile.tscn").instantiate()
		get_tree().get_root().add_child(rock)
		rock.global_position = global_position
		
		# 设置石头属性
		var direction = (target_player.global_position - global_position).normalized()
		rock.direction = direction
		rock.speed = 180.0
		rock.damage = throw_damage
		rock.source = self
		
		# 重置投掷冷却
		can_throw = false
		throw_timer = 0.0
		
		print("哥布林投掷石头!")

# 重写攻击方法，近距离时使用近战攻击
func attack_player():
	if global_position.distance_to(target_player.global_position) <= 50:
		# 近战攻击
		if animation_player and not animation_player.is_playing():
			if animation_player.has_animation("attack"):
				animation_player.play("attack")
				
				# 对玩家造成伤害
				if target_player.has_method("take_damage"):
					target_player.take_damage(damage)
			else:
				# 如果没有攻击动画，使用普通攻击
				super.attack_player()
	else:
		# 远程攻击由_physics_process处理
		pass
